package com.haiyu.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.TalkMapper;
import com.haiyu.manager.dto.TalkDTO;
import com.haiyu.manager.pojo.Talk;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.TalkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TalkServiceImpl implements TalkService {
    @Autowired
    TalkMapper talkMapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public PageDataResult getTalkList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<TalkDTO> talks = talkMapper.getTalkList();

        System.out.println("talks有" + talks);
        PageHelper.startPage(pageNum, pageSize);

        if (talks.size() != 0) {
            PageInfo<TalkDTO> pageInfo = new PageInfo<>(talks);
            pageDataResult.setList(talks);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> delTalk(Integer talkId, Integer houtaiStatus) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除
            int result = talkMapper.updateTalkStatus(talkId,houtaiStatus);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除帖子失败");
                logger.error("删除帖子失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除帖子成功");
            logger.info("删除帖子成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除帖子异常！", e);
        }
        return data;
    }

    @Override
    public Talk getTalkById(Integer talkId) {
        return talkMapper.selectByPrimaryKey(talkId);
    }
}
